package com.wlb365.financing.modules.trade.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlb365.financing.common.constants.SysConstant;
import com.wlb365.financing.common.dto.R;
import com.wlb365.financing.modules.esign.service.ESignService;
import com.wlb365.financing.modules.sys.service.IDeptService;
import com.wlb365.financing.modules.sys.service.IDictService;
import com.wlb365.financing.modules.sys.service.IUserService;
import com.wlb365.financing.modules.trade.response.OrgDataDto;
import com.wlb365.financing.modules.trade.response.QueryBankListRes;
import com.wlb365.financing.modules.trade.response.VoucherDetailRes;
import com.wlb365.financing.modules.trade.service.IAuditService;
import com.wlb365.financing.modules.trade.service.IDataService;
import com.wlb365.financing.modules.trade.service.IVoucherService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 凭证融资申请管理类.
 *
 * @author zwh
 * @version 1.0
 */
@RestController
@RequestMapping("voucher/financing")
public class VoucherFinancingController {
    @Autowired
    private IVoucherService voucherService;
    @Autowired
    private IDataService dataService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private IDictService dictService;
    @Autowired
    private IAuditService auditService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ESignService eSignService;

    /**
     * 凭证融资申请列表页面.
     * @param
     * @author zwh
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequiresPermissions("voucher:financing:index")
    @GetMapping(value = "index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("financing/voucher_financing.html");
        mv.addObject("supplierList", deptService.listDeptByType(SysConstant.DEPT_TYPE_0));
        mv.addObject("coreList", deptService.listDeptByType(SysConstant.DEPT_TYPE_1));
        mv.addObject("bankList", deptService.listDeptByType(SysConstant.DEPT_TYPE_2));
//        mv.addObject("dictList", dictService.listAllDictItemByCode("VOUCHER"));
        return mv;
    }

    /**
     * 选择银行页面.
     * @param compId 核心企业机构id
     * @author zwh
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequiresPermissions("voucher:financing:send")
    @GetMapping(value = "/bankView")
    public ModelAndView bnakView(String compId) {
        ModelAndView mv = new ModelAndView("financing/voucher_bank");
        mv.addObject("compId", compId);
        return mv;
    }

    @GetMapping(value = "/bankList")
    public R getBasnkList(String compId) {
        QueryBankListRes res = voucherService.queryBankList(compId);
        Page<OrgDataDto> page = new Page<>();
        page.setRecords(res.getItems());
        page.setSize(res.getItems().size());
        page.setTotal(res.getItems().size());
        page.setCurrent(1);
        return R.ok(page);
    }

    /**
     * 发送凭证.
     * @param id
     * @author zwh
     * @return com.wlb365.financing.common.dto.R
     **/
    @RequiresPermissions("voucher:financing:send")
    @GetMapping(value = "sendView")
    public ModelAndView sendView(String id, String bankId) {
        ModelAndView mv = new ModelAndView("financing/voucher_send.html");
        VoucherDetailRes res = voucherService.detail(id);
        res.setBankId(bankId);
        mv.addObject("editInfo", res);
        mv.addObject("coreList", deptService.listDeptByType(SysConstant.DEPT_TYPE_1));
        mv.addObject("bankList", deptService.listDeptByType(SysConstant.DEPT_TYPE_2));
        return mv;
    }

    /** 发送凭证.
    * @param id
    * @author zwh
    * @return com.wlb365.financing.common.dto.R
    **/
    @RequiresPermissions("voucher:financing:send")
    @PostMapping(value = "send")
    public R send(String id, String bankId) {
        return R.ok(voucherService.send(id, bankId));
    }

    /**
     * 凭证融资已申请列表页面.
     * @param
     * @author zwh
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequiresPermissions("voucher:financing:after")
    @GetMapping(value = "after")
    public ModelAndView financingAfter() {
        ModelAndView mv = new ModelAndView("financing/voucher_financing_after");
        mv.addObject("supplierList", deptService.listDeptByType(SysConstant.DEPT_TYPE_0));
        mv.addObject("coreList", deptService.listDeptByType(SysConstant.DEPT_TYPE_1));
        mv.addObject("bankList", deptService.listDeptByType(SysConstant.DEPT_TYPE_2));
//        mv.addObject("dictList", dictService.listAllDictItemByCode("VOUCHER"));
        return mv;
    }


}
